package datastructure.tree.threadedbinarytree;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-30
 */
public class ThreadedBinaryTree {
    public static void main(String[] args) {

    }
}

/**
 * 二叉树
 */
class BinaryTree {

    private HeroNode root;
    private HeroNode pre = null;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    /**
     * 中序线索化
     * 线索化二叉树
     *
     * @param node 需要线索化结点
     */
    public void threadedNodes(HeroNode node) {
        if (node == null) {
            return;
        }
        // 1、先线索化左子树
        threadedNodes(node.getLeft());
        // 2、线索化当前结点 处理前驱结点
        if (node.getLeft() == null) {
            node.setLeft(pre);
            node.setLeftType(1);
        }
        // 处理后继结点
        if (pre != null && pre.getRight() == null) {
            pre.setRight(node);
            pre.setRightType(1);
        }
        // 每次处理一个结点就让当前结点成为下一个结点的前驱结点
        pre = node;
        // 3、然后线索化右子树
        threadedNodes(node.getRight());
    }

    public void threadedList(){
        // 定义变量从root开始
        HeroNode node = root;
        while (node != null){
            while (node.getLeftType() == 0){
                node = node.getLeft();
            }
            System.out.println(node);
            while (node.getRightType() == 1){
                node = node.getRight();
                System.out.println(node);
            }
            node = node.getRight();
        }
    }


}

/**
 * 结点
 */
class HeroNode {
    private int id;
    private String name;
    private HeroNode left;
    private HeroNode right;

    /**
     * 0：表示左子树 1：表示前驱结点
     */
    private int leftType;
    /**
     * 0：表示右子树 1：表示后继结点
     */
    private int rightType;

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public HeroNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

}